package com.it.jpa.service.serviceImpl;

import com.it.jpa.domain.Contract;
import com.it.jpa.model.ApiModel;
import com.it.jpa.repository.ContractRespository;
import com.it.jpa.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @Author LHN
 * @ClassName ContractServiceImpl
 * @Date 2021/10/23
 * @Description }
 */
@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    ContractRespository contractRespository;

    @Override
    @Transactional
    public ApiModel insert(Contract contract) {
        Contract save = contractRespository.save(contract);
        if(save!=null){
            return ApiModel.ok();
        }
        return ApiModel.fail();
    }

    @Override
    @Transactional
    public ApiModel updateById(String contactName, Integer id) {
        Integer integer = contractRespository.updateById(contactName, id);
        if(integer>0){
            return ApiModel.ok();
        }
        return ApiModel.fail();
    }

    @Override
    @Transactional
    public ApiModel deleteById(Integer id) {
        contractRespository.deleteById(id);
        return ApiModel.ok("删除成功");
    }

    @Override
    public ApiModel findAll() {
        List<Contract> list = contractRespository.findAll();
        if(!CollectionUtils.isEmpty(list)){
            return ApiModel.ok(list);
        }
        return ApiModel.fail();
    }

    @Override
    public ApiModel findByCondition(String contractNo, String contractType, String contactName) {
        Contract contract = contractRespository.findByCondition(contractNo, contractType, contactName);
        if(contract!=null){
            return ApiModel.ok(contract);
        }
        return ApiModel.fail();
    }

    /**
     * 通过合同状态和合同开始时间在本月内的条件查询
     **/
    @Override
    public ApiModel findByContractStatus(Boolean contractStatus) {
        List<Contract> list = contractRespository.findByContractStatus(contractStatus);
        if(!CollectionUtils.isEmpty(list)){
            return ApiModel.ok(list);
        }
        return ApiModel.fail();
    }

    @Override
    public ApiModel findByPage(Integer page, Integer PageSize, String contractNo) {
        List<Contract> list = contractRespository.findByPage((page-1)*PageSize,PageSize,contractNo);
        if(!CollectionUtils.isEmpty(list)){
            return ApiModel.ok(list);
        }
        return ApiModel.fail();
    }
}
